package com.ai.aimanageapi.service;

import com.ai.aimanageapi.entity.Menu;
import com.ai.aimanageapi.repository.MenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MenuService {

    @Autowired
    private MenuRepository menuRepository;

    // 添加菜单
    public Menu addMenu(Menu menu) {
        return menuRepository.save(menu);
    }

    // 获取所有菜单
    public List<Menu> getAllMenus() {
        return menuRepository.findAll();
    }

    // 根据 ID 获取菜单
    public Optional<Menu> getMenuById(Long id) {
        return menuRepository.findById(id);
    }

    // 更新菜单
    public Menu updateMenu(Long id, Menu menuDetails) {
        Optional<Menu> optionalMenu = menuRepository.findById(id);
        if (optionalMenu.isPresent()) {
            Menu menu = optionalMenu.get();
            menu.setName(menuDetails.getName());
            menu.setUrl(menuDetails.getUrl());
            return menuRepository.save(menu);
        }
        return null;
    }

    // 删除菜单
    public void deleteMenu(Long id) {
        menuRepository.deleteById(id);
    }
}